﻿package _4_ListAndComboBox;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.ComboBoxEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 * 2011-6-5 0:33:50
 * 
 * @author Administrator
 */
public class TJComboBox2 {
    JFrame frame;
    Container contentPane;
    JComboBox<Integer> comboBox;
    JLabel label;

    public TJComboBox2() {
        frame = new JFrame("ComboBox测试");
        contentPane = frame.getContentPane();
        contentPane.setLayout(new GridBagLayout());
        setLabel();
        add(contentPane, label, 0, 0, 1, 1);
        setComboBox();
        add(contentPane, comboBox, 0, 1, 1, 0);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void setLabel() {
        label = new JLabel();
        label.setText("Swing 目前字体大小:" + label.getFont().getSize());
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        label.setBackground(Color.red);
        label.setForeground(Color.green);
        label.setOpaque(true);
        Font font = new Font("楷体", Font.PLAIN, 100);
        // label.setFont(font);
        String str = new String("Swing 目前字体大小:100");
        Rectangle2D rect = font.getStringBounds(str, new FontRenderContext(new AffineTransform(), true, true));
        label.setPreferredSize(new Dimension((int) rect.getWidth(), (int) rect.getHeight()));
    }

    public void setComboBox() {
        Integer size[] = { 10, 11, 12, 13, 14, 15, 20, 30, 40, 50 };
        DefaultComboBoxModel<Integer> model = new DefaultComboBoxModel<>(size);
        comboBox = new JComboBox<Integer>(model);
        comboBox.addItemListener(new ItemListener() {
            Object lastValue;

            public void itemStateChanged(ItemEvent e) {
                Object o = comboBox.getSelectedItem();
                if (lastValue != null) {
                    if (lastValue.equals(o)) {
                        return;
                    }
                }
                lastValue = o;
                int size;
                if (o instanceof String) {
                    try {
                        size = Integer.parseInt((String) o);
                    } catch (NumberFormatException nfe) {
                        JOptionPane.showMessageDialog(frame, "输入的大小不是纯数字", "错误提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                } else {
                    size = (Integer) o;
                }
                DefaultComboBoxModel<Integer> model = 
                        (DefaultComboBoxModel<Integer>)comboBox.getModel();
                if (model.getIndexOf(size) == -1) {
                    model.addElement(size);
                }
                label.setText("Swing 目前字体大小:" + size);
                label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), size));
            }
        });
        comboBox.setEditable(true);
        ComboBoxEditor editor = comboBox.getEditor();
        comboBox.configureEditor(editor, "请输入或者直接选择");
    }

    public void add(Container main, Container child, int gridx, int gridy, double weightx, double weighty) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = weightx;
        c.weighty = weighty;
        c.fill = GridBagConstraints.BOTH;
        main.add(child, c);
    }

    public static void main(String args[]) {
        new TJComboBox2();
    }
}
